package com.yljk.yljkproject.utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Date;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 帮助类
 */
public class MedicomUtils {


    public final static String PASSS_URL = "https://dcontrol.yilijk.com";

    /**
     * PASS合理用药地址
     */
    public final static String PASS_SCREEN_URL = PASSS_URL + "/mobile_js/app.html";


    /**
     * 获取刷新session
     */
    public final static String PASS_JWT_URL = PASSS_URL + "/passvt/pass/api/jwtResult";


    /**
     * 刷新认证方法
     */
    public final static String PASS_REFRESH_SESSION_URL = PASSS_URL + "/passvt/pass/api/saveAuthenticationInfo";


    /**
     * 美康授权授权的  sessionId
     */
    public static final String SESSIONID = "506818834";//美康授权授权的  sessionId

    /**
     * 美康授权授权的刷新session
     */
    public static String REFRESH_SESSIONID = "";//美康授权授权的刷新 session


    /**
     * <ul>
     * <li>方法名： execute </li>
     * <li>功能描述： HIS登录的时候，调用登录授权 </li>
     * <li>创建时间    2021/5/12 14:14   </li>
     * </ul>
     *
     * @param hisUser    医院编码 （不能为空）
     * @param doctorcode 医生编码（不能为空）
     * @param sessionID  美康授权的id（不能为空）
     * @return Prompt
     */
    public static String loginSign(String hisUser, String doctorcode, String sessionID) throws JSONException {
        getJwtResult(hisUser, doctorcode, sessionID);
        return "";

    }

    /**
     * <ul>
     * <li>方法名： getJwtResult </li>
     * <li>功能描述： 第一步获取 刷新 session </li>
     * <li>创建时间    2021/5/12 14:14   </li>
     * </ul>
     *
     * @param hisUser    医院编码 （不能为空）
     * @param doctorcode 医生编码（不能为空）
     * @param sessionID  美康授权的id（不能为空）
     * @return Prompt
     */
    public static String getJwtResult(String hisUser, String doctorcode, String sessionID) throws JSONException {

        JSONObject userinfo = new JSONObject();
        userinfo.put("hisUser", hisUser);
        userinfo.put("doctorcode", doctorcode);
        userinfo.put("sessionID", sessionID);

        FormBody.Builder builder = new FormBody.Builder();
        //hisUser   doctorcode   sessionID
        builder.add("psJSONStr", userinfo.toString());


        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody formBody = builder.build();
        Request request = new Request.Builder().url(MedicomUtils.PASS_JWT_URL).post(formBody).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //请求失败的处理
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    MedicomUtils.REFRESH_SESSIONID = response.body().string();
                    System.out.println(MedicomUtils.REFRESH_SESSIONID);

                    refresh();
                } else {
                    throw new IOException("Unexpected code " + response);
                }

            }
        });


        return MedicomUtils.REFRESH_SESSIONID;

    }

    /**
     * <ul>
     * <li>方法名： refresh </li>
     * <li>功能描述： 第二步 注册信息 </li>
     * <li>创建时间    2021/5/12 14:14   </li>
     * </ul>
     *
     * @return Prompt
     */
    public static String refresh() {
        if (null == MedicomUtils.REFRESH_SESSIONID || "".equals(MedicomUtils.REFRESH_SESSIONID.trim())) {
            System.out.println("未能获取到 JWT");
            return "";
        }
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("psJSONStr", MedicomUtils.REFRESH_SESSIONID);


        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody formBody = builder.build();
        Request request = new Request.Builder().url(MedicomUtils.PASS_REFRESH_SESSION_URL).post(formBody).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //请求失败的处理
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    System.out.println(response.body().string());
                } else {
                    throw new IOException("Unexpected code " + response);
                }

            }
        });


        return MedicomUtils.REFRESH_SESSIONID;

    }

}
